import com.mit.web.business.proxy.DesignProxy;
import com.mit.web.business.remote.IInvokeResponder;
import com.mit.web.config.Constants;
import com.mit.web.event.PopWinCloseEvent;
import com.mit.web.util.LogUtil;
import com.mit.web.view.component.monitor.IProgressMonitor;
import com.mit.web.vo.design.FabricVO;
import com.mit.web.vo.design.StyleVO;

import flash.events.MouseEvent;

import mx.collections.ArrayCollection;
import mx.events.FlexEvent;
import mx.logging.ILogger;
import mx.rpc.events.ResultEvent;

[Bindable] private var _sample:StyleVO;
private var _proxy:DesignProxy = DesignProxy.getInstance();
private var _pm:IProgressMonitor;
private var _logger:ILogger;

public function set sample(s:StyleVO):void{
	_sample = s;
}

//Load Items
private function loadDetailsHandler(event:FlexEvent):void{
	_logger = LogUtil.getLogger(this);
	
	var id:int = _sample.id;
	var rsp:IInvokeResponder = _proxy.getStyleById(id)
	rsp.addResultListener(loadResultHandler);
	rsp.addFaultListener(exceptionHandler);
}

private function loadResultHandler(event:ResultEvent):void{
	var s:StyleVO = event.result as StyleVO;
	_sample.primaryFab = s.primaryFab;
	_sample.secondaryFab = s.secondaryFab;
	//复制时间戳，否则会出错
	//另外一个处理办法就是在save完成的时候更新datagrid数据源的vo的时间戳。
	_sample.lastUpdatedTime = s.lastUpdatedTime;
}

//Save operation
private function doSave(event:MouseEvent):void{
	if(!validate()){
		event.preventDefault();
		return;
	}
	
	//assimble
	_sample.estimatedFee = txt_fee.number;
	_sample.sampleState = chk_status.selectedValue;
	_sample.sampleWorker = lst_worker.selectedId;
	_sample.sampleWorkerName = lst_worker.selectedItem.label;
	
	var r:IInvokeResponder = _proxy.saveSampleTask(_sample);
	r.addResultListener(saveResultHandler);
	r.addFaultListener(exceptionHandler);
}

private function validate():Boolean{
	var isValid:Boolean = true;
	if(lst_worker.selectedIndex == -1){
		showRequiredErrorTip( lst_worker);
		isValid = false;
	}
	//如果状态设置为已完成，要求工价,主料，辅料的用量必须填写
	if(isValid && chk_status.selectedValue == Constants.CLOSE){
		if(txt_fee.number == 0          ){
			showRequiredErrorTip( txt_fee);
			isValid = false;
		}
		if(isValid){
			for each(var f:FabricVO in _sample.primaryFab){
				if(f.amount == 0){
					showVerticalMiddleErrorTipAt("未填写完整", _tab);
					_tab.selectedIndex = 0;
					isValid= false;
					break;
				}
			}
		}
		if(isValid){
			for each(var s:FabricVO in _sample.secondaryFab){
				if(s.amount == 0){
					showVerticalMiddleErrorTipAt("未填写完整", _tab);
					_tab.selectedIndex = 1;
					isValid= false;
					break;
				}
			}
		}		
	}
	
	return isValid;	
}
private function saveResultHandler(event:ResultEvent):void{
	
	var e:PopWinCloseEvent = new PopWinCloseEvent(PopWinCloseEvent.SAVED);
	e.args={estimatedFee:_sample.estimatedFee, sampleState:_sample.sampleState
		,sampleWorker:_sample.sampleWorker,sampleWorkerName:_sample.sampleWorkerName};

	e.msg = ResourceManager.getInstance()
		.getString(Constants.BUNDLE_COMMON,"info.common.saved",[_sample.code]);
	this.dispatchEvent(e);

	closeWindow(this);
}

//试版通过
private function doApprove(event:MouseEvent):void{
	
	
	//assimble
	_sample.dressState = Constants.CLOSE;
	
	var r:IInvokeResponder = _proxy.approveSample(_sample);
	r.addResultListener(approveResultHandler);
	r.addFaultListener(exceptionHandler);
}
private function approveResultHandler(event:ResultEvent):void{
	
	var e:PopWinCloseEvent = new PopWinCloseEvent(PopWinCloseEvent.APPROVED);
	e.args={dressState:_sample.dressState};
	
	e.msg = ResourceManager.getInstance()
		.getString(Constants.BUNDLE_COMMON,"info.common.saved",[_sample.code]);
	this.dispatchEvent(e);
	
	closeWindow(this);
}
